import sys
import json
import numpy as np

dtype_str = ""
with open('cumsum.json', 'r') as file:
    data = json.load(file)
    dtype_str = data["outputs"][0]["dtype"]

print(f"==zf=={dtype_str}==\n")
if dtype_str == "float16":
    loss = 1e-3    # 容忍偏差，一般fp16要求绝对误差和相对误差均不超过千分之一
else:
     loss = 1e-4    # 容忍偏差，一般fp32要求绝对误差和相对误差均不超过万分之一
minimum = 10e-10


def print_diff(real_result, golden):
    close = np.isclose(real_result, golden, rtol=loss, atol=1e-8, equal_nan=True)
    indices = np.where(~close)[0][:20]
    for i in indices:
        print(f"index: {i}, real_result:{real_result[i]}, golden:{golden[i]}")


def verify_result(real_result, golden):
    real_result = np.fromfile(real_result, dtype_str)                   # 从bin文件读取实际运算结果
    golden = np.fromfile(golden, dtype_str)                             # 从bin文件读取预期运算结果
    result = np.abs(real_result - golden)                                     # 计算运算结果和预期结果偏差
    deno = np.maximum(np.abs(real_result), np.abs(golden))                    # 获取最大值并组成新数组
    result_atol = np.less_equal(result, loss)                                 # 计算绝对误差
    result_rtol = np.less_equal(result / np.add(deno, minimum), loss)         # 计算相对误差
    if not result_rtol.all() and not result_atol.all():
        if np.sum(result_rtol == False) > real_result.size * loss and np.sum(
            result_atol == False
        ) > real_result.size * loss:                                          # 误差超出预期时返回打印错误，返回对比失败
            print("[ERROR] result error")
            print_diff(real_result, golden)
            return False
    print("test pass")
    return True


if __name__ == '__main__':
    verify_result(sys.argv[1], sys.argv[2])
